import QtQuick 2.2
import QtQuick.Controls 2.0
import QtQuick.Dialogs 1.2
import "./images"
import "./Middle"
Rectangle {
    property list<Rectangle> uilist
    //property list<Loader> pagelist
    property var pagelist: new Array     //页面列表
    id: middle
    signal hideMenu()
    signal toTail(string content, string picpath)
    signal closepage(int index)
    border.color: "black"; border.width: 1

    //点击头部的东西时中间部位显示的内容 select：0表示菜单
    function showPage(index)
    {
        console.log("Middle.showpage(index)")
        for(var i = 0; i< 6; i++)
        {
            if(i === index)
                pagelist[i].z = 0.9
            else
                pagelist[i].z = 0.5

            console.log("pagelist["+i+"].z = " + pagelist[i].z)
        }
        menu.z = 0.5
        hideMenu()


    }

    function showUI(select, flag)
    {
        console.log(qsTr("middle deal !"))
        switch(flag)
        {
        case 0:
        {
            if(select)
            {
                mid.source = ""
                // uilist.push(menu)
                menu.visible = true
                menu.z = 1
            }
            else
            {
                mid.source = "./images/mainui.png"
                menu.visible =false
            }

            break
        }

        case 2:
        {
            if(select)
            {
                mid.source = ""
                eventlog.visible = true;
                eventlog.z = 1;
            }
            else
            {
                mid.source = "./images/mainui.png"
                eventlog.visible = false;
            }
            break
        }
        }
    }
    //点击主菜单中的项时应该现实的内容
    function showMenuitem(title, picpath)
    {
        if(title === "程序编辑器")
        {
            if(isOpen(qsTr("qrc:/Middle/Programming.qml")) === -1)
            {
                if(getEmptyPage() !== -1)
                {
                    var Index = getEmptyPage()
                    console.log(qsTr("开始载入 Programming.qml"))
                    pagelist[getEmptyPage()].source = qsTr("./Middle/Programming.qml");
                    pagelist[getEmptyPage()].isLoading = true;
                    showPage(Index)
                    console.log(qsTr("发送“点击”消息给 main 转给 tail\n\t\t------Middle         "))
                    toTail(title, picpath)
                }
                else
                    messagedialog.open()
            }
        }

        else if(title === "HotEdit")
        {
            if(isOpen("qrc:/Middle/HotEdit.qml") === -1)
            {
                if(getEmptyPage() !== -1)         // == -1表示列表已经满了， != -1表示列表没有满
                {
                    Index = getEmptyPage()
                    console.log(qsTr("开始载入 HotEdit.qml"))
                    pagelist[getEmptyPage()].source = qsTr("./Middle/HotEdit.qml");
                    pagelist[getEmptyPage()].isLoading = true;
                    showPage(Index)
                    console.log(qsTr("发送“点击”消息给 main 转给 tail\n\t\t------Middle         "))
                    toTail(title, picpath)
                }
                else
                    messagedialog.open()
            }
        }
        else if(title === "备份与恢复")
        {
            if(isOpen("qrc:/Middle/Backup.qml") === -1)
            {
                if(getEmptyPage() !== -1)
                {
                    Index = getEmptyPage()
                    console.log(qsTr("开始载入 Backup.qml"))
                    pagelist[getEmptyPage()].source = qsTr("./Middle/Backup.qml");
                    pagelist[getEmptyPage()].isLoading = true;
                    showPage(Index)
                    console.log(qsTr("发送“点击”消息给 main 转给 tail\n\t\t------Middle         "))
                    toTail(title, picpath)
                }
                else
                    messagedialog.open()
            }
        }
        else if(title === "输入输出")
        {
            if(isOpen("qrc:/Middle/InandOut.qml") === -1)
            {
                if(getEmptyPage() !== -1)
                {
                    Index = getEmptyPage()
                    console.log(qsTr("开始载入 InandOut.qml"))
                    pagelist[getEmptyPage()].source = qsTr("./Middle/InandOut.qml");
                    pagelist[getEmptyPage()].isLoading = true;
                    showPage(Index)
                    console.log(qsTr("发送“点击”消息给 main 转给 tail\n\t\t------Middle         "))
                    toTail(title, picpath)
                }
                else
                    messagedialog.open()
            }
        }
        else if(title === "校准")
        {
            if(isOpen("qrc:/Middle/Correct.qml") === -1)
            {
                if(getEmptyPage() !== -1)
                {
                    Index = getEmptyPage()
                    console.log(qsTr("开始载入 Correct.qml"))
                    pagelist[getEmptyPage()].source = qsTr("./Middle/Correct.qml");
                    pagelist[getEmptyPage()].isLoading = true;
                    showPage(Index)
                    console.log(qsTr("发送“点击”消息给 main 转给 tail\n\t\t------Middle         "))
                    toTail(title, picpath)
                }
                else
                    messagedialog.open()
            }
        }
        else if(title === "手动操作")
        {
            if(isOpen("qrc:/Middle/HandOperate.qml") === -1)
            {
                if(getEmptyPage() !== -1)
                {
                    Index = getEmptyPage()
                    console.log(qsTr("开始载入 HandOperate.qml"))
                    pagelist[getEmptyPage()].source = qsTr("./Middle/HandOperate.qml");
                    pagelist[getEmptyPage()].isLoading = true;
                    showPage(Index)
                    console.log(qsTr("发送“点击”消息给 main 转给 tail\n\t\t------Middle         "))
                    toTail(title, picpath)
                }
                else
                    messagedialog.open()
            }
        }
        else if(title === "控制面板")
        {
            if(isOpen("qrc:/Middle/CtrlPanel.qml") === -1)
            {
                if(getEmptyPage() !== -1)
                {
                    Index = getEmptyPage()
                    console.log(qsTr("开始载入 CtrlPanel.qml"))
                    pagelist[getEmptyPage()].source = qsTr("./Middle/CtrlPanel.qml");
                    pagelist[getEmptyPage()].isLoading = true;
                    showPage(Index)
                    console.log(qsTr("发送“点击”消息给 main 转给 tail\n\t\t------Middle         "))
                    toTail(title, picpath)
                }
                else
                    messagedialog.open()
            }
        }
        else if(title === "自动生产窗口")
        {
            if(isOpen("qrc:/Middle/AutoProduce.qml") === -1)
            {
                if(getEmptyPage() !== -1)
                {
                    Index = getEmptyPage()
                    console.log(qsTr("开始载入 AutoProduce.qml"))
                    pagelist[getEmptyPage()].source = qsTr("./Middle/AutoProduce.qml");
                    pagelist[getEmptyPage()].isLoading = true;
                    showPage(Index)
                    console.log(qsTr("发送“点击”消息给 main 转给 tail\n\t\t------Middle         "))
                    toTail(title, picpath)
                }
                else
                    messagedialog.open()
            }
        }
        else if(title === "事件日志")
        {
            if(isOpen("qrc:/Middle/EventLog.qml") === -1)
            {
                if(getEmptyPage() !== -1)
                {
                    Index = getEmptyPage()
                    console.log(qsTr("开始载入 EventLog.qml"))
                    pagelist[getEmptyPage()].source = qsTr("./Middle/EventLog.qml");
                    pagelist[getEmptyPage()].isLoading = true;
                    showPage(Index)
                    console.log(qsTr("发送“点击”消息给 main 转给 tail\n\t\t------Middle         "))
                    toTail(title, picpath)
                }
                else
                    messagedialog.open()
            }
        }
        else if(title === "FlexPendant资源管理器")
        {
            if(isOpen("qrc:/Middle/ResManage.qml") === -1)
            {
                if(getEmptyPage() !== -1)
                {
                    Index = getEmptyPage()
                    console.log(qsTr("开始载入 ResManage.qml"))
                    pagelist[getEmptyPage()].source = qsTr("./Middle/ResManage.qml");
                    pagelist[getEmptyPage()].isLoading = true;
                    showPage(Index)
                    console.log(qsTr("发送“点击”消息给 main 转给 tail\n\t\t------Middle         "))
                    toTail(title, picpath)
                }
                else
                    messagedialog.open()
            }
        }
        else if(title === "程序数据")
        {
            if(isOpen("qrc:/Middle/ProgramData.qml") === -1)
            {
                if(getEmptyPage() !== -1)
                {
                    Index = getEmptyPage()
                    console.log(qsTr("开始载入 ProgramData.qml"))
                    pagelist[getEmptyPage()].source = qsTr("./Middle/ProgramData.qml");
                    pagelist[getEmptyPage()].isLoading = true;
                    showPage(Index)
                    console.log(qsTr("发送“点击”消息给 main 转给 tail\n\t\t------Middle         "))
                    toTail(title, picpath)
                }
                else
                    messagedialog.open()
            }
        }
        else if(title === "系统信息")
        {
            if(isOpen("qrc:/Middle/SysInfo.qml") === -1)
            {
                if(getEmptyPage() !== -1)
                {
                    Index = getEmptyPage()
                    console.log(qsTr("开始载入 SysInfo.qml"))
                    pagelist[getEmptyPage()].source = qsTr("./Middle/SysInfo.qml");
                    pagelist[getEmptyPage()].isLoading = true;
                    showPage(Index)
                    console.log(qsTr("发送“点击”消息给 main 转给 tail\n\t\t------Middle         "))
                    toTail(title, picpath)
                }
                else
                    messagedialog.open()
            }
        }

        //测试查看
        for(var i=0; i<6; i++)
        console.log("pagelist["+ i +"] = " + pagelist[i].source)

        menu.visible = false
        hideMenu()

    }

    //==================关闭视图=====================
    function closePage()
    {
        var i;
        for(i=0; i< 6; i++)
        {
            //var k = pagelist[i].z
            if(pagelist[i].z === 0.9)
            {
                reArrangeList(i)
                break
            }
            console.log("pagelist["+i+"].source = "+ pagelist[i].source)
        }
        //pagelist[i].z = 0.9


        for(var ii =0; ii< 6; ii++)
        {
            console.log("pagelist["+ii+"].source = "+ pagelist[ii].source + "\t\t z=" + pagelist[ii].z +"isLoading = "+ pagelist[ii].isLoading)
            //console.log("pagelist["+ii+"].z = "+ pagelist[ii].z)
        }
    }

    function reArrangeList(index)
    {
        var i = index
        if(i !== 0)
          //  pagelist[i].z = 0.5
        console.log(pagelist[i+1].source)
        //删除后后一个往前一个推进
        while(i < 5 && pagelist[i+1].source.toString() !== "" )
        {
//            console.log(pagelist[i+1].source)
            //            console.log("更换前pagelist["+i+"].source = "+pagelist[i].source +
            //                        "\n      pagelist["+(i+1)+"].source = "+ pagelist[i+1].source)
            pagelist[i].isLoading = pagelist[i+1].isLoading
            pagelist[i].source = pagelist[i+1].source
//            console.log("更换后pagelist["+i+"].source = "+pagelist[i].source +
//                        "\n      pagelist["+(i+1)+"].source = "+ pagelist[i+1].source)
            i++
        }
        if(pagelist[i+1].source.toString() === "")
        {
            pagelist[i-1].z = 0.9
        }

        console.log(pagelist[i-1].source)
        pagelist[i].release()
        console.log(pagelist[i-1].source)
    }
//===========================================
    //获取空闲的page Loader, 然后返回空闲的index；如果没有返回-1
    function getEmptyPage()
    {
        for(var i=0; i< 6; i++)
        {
            if(!pagelist[i].isLoading)
                return i
        }
        return -1
    }
    //检测当前页面是否已打开，若是，返回page Loader的index，如果没有，返回-1
    function isOpen(path)
    {
        for(var i=0; i< 6; i++)
        {
            if(pagelist[i].source.toString() === path)
            {
                return i
            }
        }
        return -1
    }

    Component.onCompleted: {
        console.log(qsTr("pagelist push page1——page6"))
        pagelist.push(page1)
        pagelist.push(page2)
        pagelist.push(page3)
        pagelist.push(page4)
        pagelist.push(page5)
        pagelist.push(page6)
    }

    Mainmenu{
        id: menu
        width: parent.width; height: parent.height
        visible: false
        anchors.fill: parent

        onItemclick:
        {
            console.log(qsTr("Middle 已收到“点击”消息\n\t\t------Middle         "))
            console.log(qsTr("showing programming UI"))
            showMenuitem(title, picpath)             //方法
            //信号
        }
        //        onTailItem:
        //        {
        //            console.log(qsTr("发送“点击”消息给 main 转给 tail\n\t\t------Middle"))
        //            tailItem(content, picpath)
        //        }
    }

    EventLog{
        id: eventlog;
        width: parent.width; height: parent.height;
        visible: false;
        anchors.fill: parent;
    }

    //背景图
    Image {
        id: mid
        source: "./images/mainui.png"
        anchors.centerIn: parent
    }


    //六个Loader
    // pagelist: [
    Loader{
        property bool isLoading: false
        id: page1
        function release()
        {
            isLoading = false
            source = ""
            z = 0
        }

        width: middle.width; height: middle.height
        //anchors.bottom: middle.bottom; anchors.bottomMargin: 2
        anchors.bottom: parent.bottom; anchors.bottomMargin: 2
    }

    Loader{
        property bool isLoading: false
        id: page2
        function release()
        {
            isLoading = false
            source = ""
            z = 0
        }
        width: middle.width; height: middle.height
        //anchors.bottom: middle.bottom; anchors.bottomMargin: 2
        anchors.bottom: parent.bottom; anchors.bottomMargin: 2
    }

    Loader{
        property bool isLoading: false
        id: page3
        function release()
        {
            isLoading = false
            source = ""
            z = 0
        }
        width: middle.width; height: middle.height
        //anchors.bottom: middle.bottom; anchors.bottomMargin: 2
        anchors.bottom: parent.bottom; anchors.bottomMargin: 2
    }

    Loader{
        property bool isLoading: false
        id: page4
        function release()
        {
            isLoading = false
            source = ""
            z = 0
        }
        width: middle.width; height: middle.height
        //anchors.bottom: middle.bottom; anchors.bottomMargin: 2
        anchors.bottom: parent.bottom; anchors.bottomMargin: 2
    }

    Loader{
        property bool isLoading: false
        id: page5
        function release()
        {
            isLoading = false
            source = ""
            z = 0
        }
        width: middle.width; height: middle.height
        //anchors.bottom: middle.bottom; anchors.bottomMargin: 2
        anchors.bottom: parent.bottom; anchors.bottomMargin: 2
    }

    Loader{
        property bool isLoading: false
        id: page6
        function release()
        {
            isLoading = false
            source = ""
            z = 0
        }
        width: middle.width; height: middle.height
        //anchors.bottom: middle.bottom; anchors.bottomMargin: 2
        anchors.bottom: parent.bottom; anchors.bottomMargin: 2
    }

    //   ]//pagelist
    //anchors.fill: parent
    //提示窗口
    MessageDialog{
        id: messagedialog
        title: qsTr("视图太多")
        text: qsTr("打开的视图不能超过6个！")
        detailedText: qsTr("请尝试关闭一些视图再打开新的视图。")
        icon: StandardIcon.Warning
        standardButtons: StandardButton.Ok
    }
}
